package com.imy.lua.lib.gesture;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.immomo.mls.LuaViewManager;
import com.immomo.mls.annotation.LuaBridge;
import com.immomo.mls.annotation.LuaClass;
import com.immomo.mls.wrapper.callback.IVoidCallback;
import com.imy.common.util.ALog;
import com.imy.common.util.UiUtil;
import com.imy.lua.LuaCore;
import com.imy.lua.helper.PointScaleHelper;
import com.imy.touch.GestureManager;
import com.imy.touch.TouchCore;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;

/**
 * Created by MLN Template 注册方法: Register.newUDHolderWithLuaClass(UDGesture.LUA_CLASS_NAME, UDGesture.class, true) 若想在Java层实现参数自动转换，则增加注册:
 *
 * @see com.immomo.mls.MLSBuilder#registerCovert
 * @see com.immomo.mls.MLSBuilder.CHolder new MLSBuilder.CHolder(UDGesture.class)
 */
@LuaClass(gcByLua = false)
public class UDGesture {
  /**
   * Lua类型，Lua可由此创建对象: local obj = gesture()
   */
  public static final String LUA_CLASS_NAME = "Gesture";


  /**
   * 直接在属性中增加注解，可让Lua有相关属性 eg: local obj = gesture() local p = obj:property() --获取相关值 obj:property(10)         --设置相关值
   */

  public UDGesture(@NonNull Globals g, @NonNull LuaValue[] init) {

  }



  @LuaBridge
  public void setMode(int mode) {
    TouchCore.get().setMode(mode);
  }

  @LuaBridge
  public void clicked(int x, int y,int id) {
    //转换X
    GestureManager.get().clicked(PointScaleHelper.get().changeX(x), PointScaleHelper.get().changY(y),id);
  }




  /**
   * 从某个位置滑动到某个位置
   *
   * @param startX
   * @param startY
   * @param endX
   * @param endY
   * @param step   步进 分为多少步
   * @param time   总工消耗时长
   */
  @LuaBridge
  public void swipe(int startX, int startY, int endX, int endY, int step, int time,int id) {
    GestureManager.get().swipe(PointScaleHelper.get().changeX(startX),
            PointScaleHelper.get().changY(startY),
            PointScaleHelper.get().changeX(endX),
            PointScaleHelper.get().changY(endY), step, time,id);
  }

  @LuaBridge
  public void longClicked(int x, int y, int id) {
    GestureManager.get().longClicked(PointScaleHelper.get().changeX(x),
            PointScaleHelper.get().changY(y), id);
  }

  @LuaBridge
  public void down(int x, int y, int id) {
    GestureManager.get().down(
            PointScaleHelper.get().changeX(x),
            PointScaleHelper.get().changY(y), id);
  }

  @LuaBridge
  public void up(int x, int y, int id) {
    GestureManager.get().up(PointScaleHelper.get().changeX(x),
            PointScaleHelper.get().changY(y), id);
  }

  @LuaBridge
  public void move(int x, int y, int id) {
    GestureManager.get().move(
            PointScaleHelper.get().changeX(x), PointScaleHelper.get().changY(y), id);
  }

  protected Context getContext(@NonNull Globals globals) {
    LuaViewManager m = (LuaViewManager) globals.getJavaUserdata();
    return m != null ? m.context : null;
  }

}
